home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / PTR_HELP.TXT < prev    next >
Text File  |  1993-04-05  |  11KB  |  274 lines

  1. Date:   Tue Jun 16 1992  11:05:08
  2. From:   Ted Jensen
  3. To:     Michael Halcrow
  4. Subj:   Pointers & things
  5. Attr:   
  6. international C echo
  7. -------------------------------
  8. Michael-
  9.  
  10.    In reading your messages to Mike Dalsanto, Robert Place, and
  11. All I see you need some help on pointers, arrays, and strings.
  12. Perhaps the following will help:
  13.  
  14.    A string is a one dimensional array of characters.  A one
  15. dimensional array of a given type is contiguous in memory with
  16. each element following in memory the one preceding it.
  17.  
  18.    A pointer is a variable much like any other variable.  For
  19. example, let's consider a more easily understood variable, the
  20. integer:
  21.  
  22.     int k;
  23.  
  24.     defines an integer and reserves x bytes of space in memory
  25. for its storage.  The number of bytes reserved depends on the
  26. system and/or compiler.  For DOS systems an integer requires 2
  27. bytes of storage.  Defining an integer, as above, does not give
  28. it a value.  If it is defined as a global variable, it will be
  29. intialized it to zero.  If it is defined as an auto variable
  30. (i.e. within a function) its value is indeterminate (i.e. a
  31. random value).  Before use, the safest thing to do with any
  32. variable is to intialize it to some value (though in this case
  33. most (all?) compilers will intialize it to zero if it is global.
  34.  
  35.     Now, let's look at a pointer to the type integer which can be
  36. defined as:
  37.  
  38.     int *int_ptr;
  39.  
  40.     defines a pointer and reserves x bytes of space in memory for
  41. its storage.  The number of bytes reserved depends on the system
  42. and/or compiler.  But, fundamentally, since a pointer is designed
  43. to hold an address in memory, it must reserve the number of bytes
  44. necessary to contain such an address.  Defining a pointer, as
  45. above, does not give it a value.  If it is defined as a global
  46. variable it will be initialzed to a NULL, i.e. the address it
  47. will contain (which is the same as saying "the address to which
  48. it points to") will be set to a value of NULL which is #defined
  49. in <stdio.h>.  In DOS systems, NULL is defined as the address of
  50. the first byte of the data segment.  In Borland products, the
  51. first few bytes of the data segment contain a copyright notice.
  52. Before use, a pointer _must_ be "pointed at something", i.e.
  53. assigned a value equal to the address of some data item. More on
  54. this later.
  55.  
  56.     The key to understanding pointers is to remember that they
  57. are variables which contain addresses and that when we say a
  58. pointer "points to something", we mean that it contains the
  59. address of another variable or some other point in memory (such
  60. as the video screen buffer).
  61.  
  62.     Suppose now, that I want to "point" my integer pointer,
  63. int_ptr, at my integer k.  We do this as follows:
  64.  
  65.     int_ptr = &k;
  66.  
  67.     Why use the '&'?  Well, let's say k = 7;, were we to write
  68. "int_ptr = k;" we would be "pointing our pointer" at memory
  69. location 7.  But what we want is to "point our pointer" to the
  70. memory location where the value of k is stored, which is an
  71. entirely different thing.  By preceding the 'k' with the '&' we
  72. get the that address where the value of k is stored and point the
  73. pointer where we intend.
  74.  
  75.     Now, let's move on to arrays, and eventually arrays of
  76. characters which, in some languages, are called strings.  But
  77. first, let's discuss an array of integers as in:
  78.  
  79.     int int_array[5];
  80.  
  81. defines an array capable of holding 5 integers.  We could then
  82. intialize these with statement such as:  int_array[0] = 4; ,
  83. int_array[1] = 7 , etc.  Or, we could initialize the array at the
  84. same time as we define it with:
  85.  
  86.     int int_array[5] = { 4, 7, 2, -2, 6 };
  87.  
  88.     The 5 memory locations for these integers will be contiguous
  89. in memory, i.e. if the 4 is stored at memory location 1000, it is
  90. gauranteed that the 7 will be stored at memory location 1002
  91. (recall an integer takes 2 bytes of storage) and the 2 will be
  92. stored at 1004, etc.
  93.  
  94.     Recall that we stated that &k returned the address where the
  95. value of the integer k was stored.  Similarly
  96.  
  97.         &int_array[0]    will return the memory location of the
  98. first integer in the array.  Thus, if we want to point our
  99. pointer at the array (i.e. the first element of the array) we can
  100. write:
  101.  
  102.     int_ptr = &int_array[0];
  103.  
  104. However, with arrays, there is a second option.  The name of our
  105. array will also return the memory location of the first element.
  106. This means that we can write:
  107.  
  108.     int_ptr = int_array;
  109.  
  110. and get exactly the same result.
  111.  
  112.     With that as background, let's now turn to "strings", (called
  113. character arrays in C).  Everything we said about integer arrays
  114. can also be said about character arrays.  Thus:
  115.  
  116.     char c_array[20];   /* reserves space for 20 characters */
  117.     char *c_ptr;        /* an (unitialized) pointer to a
  118.                            character */
  119.  
  120.     c_ptr = &c_array[0];    /* points the pointer at the first
  121.                                char of the array ("string") */
  122.  
  123.     c_ptr = c_array;        /* does the same */
  124.  
  125.     Note that &c_array is meaningless.
  126.  
  127.     char c_array[] = { 'M', 'i', 'c', 'h', 'a', 'e', 'l', '\0' };
  128.  
  129.     This last line intializes c_array to hold "Michael".
  130. However, with character arrays, the above line can be written
  131. using a sort of shorthand as:
  132.  
  133.     char c_array[] = "Michael";
  134.  
  135.     Note that in the later case, the space reserved will be 8
  136. bytes and the 8th byte will automatically be set to '\0' so that
  137. this syntax is, in fact, shorthand for the longer method
  138. preceding it.
  139.  
  140.     Now, let's to back to pointers and discuss them in context
  141. with functions.  Consider the following function:
  142.  
  143.     void do_something(int k, int *p);
  144.  
  145.     If you have studied functions you know that parameters are
  146. passed by _value_.  That is, if k = 7, what gets passed as a
  147. parameter is the value 7 (as distinguished from the address of
  148. k).  Similarly, what gets passed for the second parameter is the
  149. value of p.  But, the value of p is an address.  Thus, the
  150. following three calls to do_something are equivalent:
  151.  
  152.     int k = 7;
  153.     int int_arr[5] = { 1, 2, 3, 4, 5 };
  154.     int *pk;
  155.     int *pa;
  156.     pk = &k;
  157.     pa = int_arr;
  158.  
  159.     do_something( 3, pk );
  160.     do_something( 3, &k );
  161.  
  162. Similarly, these three calls to our function
  163.  
  164.     do_something( 3, pa);
  165.     do_something( 3, &int_arr[0]);
  166.     do_something( 3, int_arr);
  167.  
  168. are identical, i.e. the _value_ of the second parameter is the
  169. same in all three cases.  This frees the programmer to choose
  170. that approach which he/she is more comfortable with.
  171.  
  172. Now, let's talk about "strings" some more.  There is one more
  173. kind of "string" that we have yet to discuss.  It is called the
  174. "literal string".  A literal string is any string in your code
  175. that appears in quotes.  Thus, in all the following cases the
  176. string "l_string" is a literal string.
  177.  
  178.     char s_arr[] = "l_string";
  179.     fopen("my_file", "wb");
  180.     strcpy(s_arr, "l_string");
  181.     printf("This is a test");
  182.  
  183. During the compilation process, whenever and where ever the
  184. compiler sees a literal string it a) reserves space in the data
  185. segment to hold the string, b) puts the string within the quotes
  186. in that space and terminates it with the '\0' character, and c)
  187. replaces the literal string in the statement with a pointer
  188. "pointing to" the memory location where it is stored.
  189.  
  190. Thus, for example, if the compiler sees:
  191.  
  192.      strcpy(s_arr, "l_string");
  193.  
  194. it moves the characters "l_string\0" to a memory location in the
  195. data segment, lets say it is at DS:0205.  It then "modifies" the
  196. statement to read:
  197.  
  198.     strcpy(s_arr, DS:0205);   And if s_arr is located at DS:0097,
  199.  
  200. it becomes:
  201.  
  202.     strcpy(DS:0097, DS:0097);
  203.  
  204. Thus, if you look at the prototype for strcpy(), located in
  205. <string.h>, you will see something like:
  206.  
  207.     char *strcpy(char *p1, char *p2);
  208.  
  209. which indicates that this function should be passed "pointers".
  210. But, now we know that it _really_ means that the function should
  211. be passed the _values_ of these pointers, which are _addresses_.
  212. And because we also recognize that the compiler replaces literal
  213. strings with addresses, a statment such as:
  214.  
  215.     strcpy(s_arr, "Michael");
  216.  
  217. begins to make some sense (at least I hope it does!).
  218.  
  219. Michael, I have just scratched the surface here.  Other things
  220. that need "going into" include how and why the compiler _always_
  221. converts array notation to pointer notation as in converting:
  222.  
  223.     a[n]     to       *(a + n);
  224.  
  225. Why are arrays always based at zero?  i.e. why is the first
  226. element of an array always referred to as array[0] and not
  227. array[1]?  Why does something like:
  228.  
  229.     char *p = "This is a test";
  230.  
  231. initialize a string much the same as;
  232.  
  233.     char arr[] = "This is a test";   but with different results.
  234.  
  235. There are dozens of other issues that you will learn as you gain
  236. experience.  In the long run, you will find that C is much more
  237. powerful than BASIC.  But, having the power makes it more
  238. difficult to use.  One can add, subtract, multiply or divide with
  239. pencil and paper, and this is the way we all learn in the
  240. beginning.  A little later we learn there is a machine that we
  241. can use to do this called a calculator, and this does things
  242. faster.  Further more, calculators come in various degrees of
  243. complexity, including some that can be programmed.  When using
  244. the more complex calculators we must have a little more knowledge
  245. and take a little more care to avoid errors.
  246.  
  247. As we move up this chain of experience we eventually come to
  248. computers where, once again, we can still add, subtract,
  249. multiply, and divide, but to do so (in a program) requires still
  250. more knowledge and care.  With each increase in power comes the
  251. need for more knowledge and the attainment of more flexibility
  252. (ever try and do wordprocessing on a four function calculator?).
  253.  
  254. If you want to use sharper knives to make your task easier, you
  255. just have to be more careful not to cut yourself since the cut
  256. will go a lot deeper!
  257.  
  258. At the point you are in your development of skills with C, I
  259. _strongly_ recommend you purchase:
  260.  
  261.     "The C Programming Language"   (2nd edition)
  262.      Kernighan and Ritchie
  263.      Prentice Hall
  264.  
  265. This is the 2nd edition of the book that started it all.  The
  266. authors were the ones who developed the C language and the book,
  267. often referred to as K&R or K&R2, is the _true_ bible for C
  268. users.  If you start at page 1, take it slow, and read it
  269. carefully, it will really pay off and it won't be any time at all
  270. before you are answering the questions here that you are now
  271. asking!
  272.  
  273. Hope this helps!   ... and good luck!
  274.